package com.bjgzy.rating.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.di.component.DaggerMyWorksComponent;
import com.bjgzy.rating.di.module.MyWorksModule;
import com.bjgzy.rating.mvp.contract.MyWorksContract;
import com.bjgzy.rating.mvp.presenter.MyWorksPresenter;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.EntriesData;
import com.eduhzy.ttw.commonsdk.entity.MyWorksData;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-30 20:32:07
 */
@Route(path = RouterHub.RATING_MYWORKSACTIVITY)
public class MyWorksActivity extends BaseActivity<MyWorksPresenter> implements MyWorksContract.View {
    @Autowired(name = Constants.ROUTER_DATA)
    RatingHomeData data;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R2.id.btn_submit)
    QMUIRoundButton mBtnSubmit;

    @Inject
    BaseQuickAdapter<MyWorksData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerMyWorksComponent
                .builder()
                .appComponent(appComponent)
                .myWorksModule(new MyWorksModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_rating_mywork");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.rating_activity_my_works;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        mBtnSubmit.setChangeAlphaWhenPress(true);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(mLayoutManager);
        mSwipeLayout.setOnRefreshListener(() -> getData(true));
        mAdapter.setOnLoadMoreListener(() -> getData(false), mRvList);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MyWorksData worksData = mAdapter.getData().get(position);
            if (worksData.getStatus() >= 0 && worksData.getStatus() < 2) {
                EntriesData entriesData = new EntriesData();
                entriesData.setResearchId(worksData.getResearchId());
                entriesData.setWorksId(worksData.getWorksId());
                entriesData.setFileType(worksData.getFileType());
                TextView no = view.findViewById(R.id.tv_no);
                TextView name = view.findViewById(R.id.tv_name);
                ARouter.getInstance()
                        .build(RouterHub.RATING_ENTRIESDETAILACTIVITY)
                        .withParcelable(Constants.ROUTER_DATA, entriesData)
                        .withBoolean(Constants.ROUTER_BOOLEAN, true)
                        .withString(Intent.EXTRA_TITLE, no.getText().toString() + name.getText().toString())
                        .navigation();
            }

        });
        mAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            MyWorksData worksData = mAdapter.getData().get(position);
            if (worksData.getStatus() >= 0 && worksData.getStatus() < 2) {

            }
            return false;
        });
        getData(true);
    }

    private void getData(boolean pullToRefresh) {
        if (ObjectUtils.isNotEmpty(data)) {
            mPresenter.getMyWorks(pullToRefresh, data.getId());
        }
    }

    @Override
    public void showLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update() {
        if (ObjectUtils.isNotEmpty(data)&&ObjectUtils.isNotEmpty(data.getResearchWorksItem())) {
            if (mAdapter.getData().size() < data.getResearchWorksItem().getMaxNum()) {
                findViewById(R.id.layout_submit).setVisibility(View.VISIBLE);
            }
        }
    }


    @OnClick(R2.id.btn_submit)
    public void onViewClicked() {
        if (data.getResearchWorksItem().getFileType() == Constants.FILETYPE.DOCUMENT) {
            showMessage("手机端不支持参加此类型活动");
        } else {
            Utils.customNavigation(RouterHub.RATING_SIGNUP1ACTIVITY, Constants.CHOOSEGROUP, data);
        }
    }

    @Subscriber(tag = RouterHub.RATING_MYWORKSACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.WORKS_UPLOAD_SUCCESS:
                killMyself();
                break;
            default:
        }
    }
}
